<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="视频位置" prop="viPlace">
        <el-select
          v-model="queryParams.viPlace"
          @change="change"
          placeholder="视频位置"
        >
          <el-option label="首页" value="0"></el-option>
          <el-option label="商学院" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="viShow">
        <el-select
          v-model="queryParams.viShow"
          @change="change"
          placeholder="是否显示"
        >
          <el-option label="显示" value="0"></el-option>
          <el-option label="隐藏" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:slideshow:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="videoListData"
      @selection-change="handleSelectionChange"
      height="800"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="视频名称" align="center" prop="viTitle" />
      <el-table-column label="视频" align="center" prop="slPic">
        <template slot-scope="{ row }">
          <video
            :src="getVideo(row.viUrl)"
            alt=""
            controls
            style="height: 200px"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="viShow" />
      <el-table-column label="视频位置" align="center" prop="viPlace" />
      <el-table-column label="描述" align="center" prop="viDescribe" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:slideshow:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:slideshow:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="视频名称" prop="viTitle">
          <el-input v-model="form.viTitle" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item label="是否显示" prop="viShow">
          <el-radio-group v-model="form.viShow">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="视频位置" prop="viPlace">
          <el-radio-group v-model="form.viPlace">
            <el-radio :label="0">首页</el-radio>
            <el-radio :label="1">商学院</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="viDescribe">
          <el-input v-model="form.viDescribe" placeholder="描述内容" />
        </el-form-item>
        <el-form-item label="视频" prop="slPic">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :on-error="handleError"
            :before-upload="beforeAvatarUpload"
            accept="video/mp4, video/ogg, video/*"
          >
            <video
              v-if="urlVideo"
              :src="urlVideo"
              class="avatar video-avatar"
              controls="controls"
            >
              您的浏览器不支持视频播放
            </video>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  videoList,
  editVideo,
  getVideo,
  addVideo,
  deleteVideo,
} from "@/api/video/index";
import { getToken } from "@/utils/auth";
import { getListFranchisee } from "@/api/mini/icon";
export default {
  name: "slideshow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序首页轮播图表格数据
      videoListData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        viPlace: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        slConceal: [
          {
            required: true,
            message: "显示 0显示 1隐藏不能为空",
            trigger: "blur",
          },
        ],
        slStatus: [
          {
            required: true,
            message: "状态 0正常 1删除不能为空",
            trigger: "change",
          },
        ],
      },
      ListFranchisee: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      urlVideo: "",
      videoFlag: false,
      //是否显示进度条
      videoUploadPercent: "",
      //进度条的进度，
      isShowUploadVideo: false,
    };
  },
  created() {
    this.getListFran();
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      videoList(this.queryParams).then((response) => {
        response.rows.map((item) => {
          item.viShow = item.viShow == 0 ? "显示" : "隐藏";
          item.viPlace = item.viPlace == 0 ? "首页" : "商学院";
        });
        this.videoListData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上传视频
    handleAvatarSuccess(res, file) {
      this.form.viUrl = res.fileName;
      this.urlVideo = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isLt50M = file.size / 1024 / 1024 < 500;
      if (!isLt50M) {
        this.$message.error("上传头像视频大小不能超过 500MB!");
      }
      return isLt50M;
    },
    getVideo(url) {
      return this.baseUrl + url;
    },
    getListFran() {
      getListFranchisee().then((response) => {
        response.rows.map((item) => {
          item.label = item.leName;
          item.value = item.id;
        });
        this.ListFranchisee = response.rows;
      });
    },
    getJms(code) {
      return this.selectDictLabel(this.ListFranchisee, code);
    },
    handleError(err, file, fileList) {
      // 处理上传错误
      console.error("Upload error:", err);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    change() {
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        slName: null,
        slPic: null,
        slOrder: null,
        slConceal: true,
        slStatus: null,
        creationTime: null,
        creationBy: null,
        viShow: 0,
        viPlace: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序首页轮播图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVideo(id).then((res) => {
        console.log(res);
        this.form = res.data;
        this.title = "修改";
        this.urlVideo = this.baseUrl + res.data.viUrl;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            editVideo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          this.slPic = "";
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除小程序首页轮播图编号为"' + ids + '"的数据项？')
        .then(function () {
          return deleteVideo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/slideshow/export",
        {
          ...this.queryParams,
        },
        `slideshow_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
